package com.gongjian.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.gongjian.database.entity.Activity;
import com.gongjian.database.entity.Register;
import com.gongjian.database.entity.User;
import com.gongjian.database.service.WxUserService;
import com.gongjian.utile.CodeEntity;
import com.gongjian.web.register.ResgisterInfo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import java.sql.Timestamp;
import java.util.Date;


/**
 * 微信接口
 * @author 董浩
 * @Description
 * @create 2019-03-21 14:42
 */
@RestController
public class Wx {
    //签到时间差
    long registerTimesub=5000;
    //精度差
    double latitudeSub=0.1;
    //维度差
    double longitudeSub=0.1;


    @Autowired
    WxUserService userService;
    @Autowired
    SqlSession sqlSession;
    /**
     * 微信绑定接口
     * @param code
     * @param userNumber
     * @param userName
     * @return
     */
    @RequestMapping("/wx/bindWx")
    public String login(String code,String userNumber,String userName){
        return userService.bindWx(code,userNumber,userName);
    }
    /**
     * 普通用户微信登录接口
     * @param code
     * @param session
     * @return
     */
    @RequestMapping("/wx/login")
    public String login(String code,HttpSession session){
        return userService.Wxlogin(code,session);
    }

    /**
     *  后台扫码登录
     * @param code
     * @param session
     * @return
     * D31E0CDD7DDCC2D703B736B67BCB2D32
     * D31E0CDD7DDCC2D703B736B67BCB2D32
     */
    @RequestMapping("/wx/backLogin")
    public String backLogin(String code,double latitude,double longitude,HttpSession session)
    {

        System.out.println(session.getId()+">>>>>");
        return userService.WxBackLogin(latitude,longitude,code,session);
    }

    /**
     * 开始签到的 生成二维码 老师扫 生成位置信息
     * 由微信端扫描
     *
     *  latitude: that.data.latitude,//精度
     *  longitude: that.data.longitude,//纬度
     * @return
     */
    @RequestMapping("/wx/registerRQ")
    public String kaisi(double latitude,double longitude,HttpSession session){
        System.out.println(latitude+"="+longitude);
       CodeEntity codeEntity= new CodeEntity(200,"ok");

    //   codeEntity.put("session",session.getId());
        return codeEntity.toJSONString();
    }

    /**
     * 签到
     * @param session
     * @return
     */
    @RequestMapping("/wx/register")
    public String Rsgister(String data,HttpSession session){

        //System.out.println("ssss>>>"+session.getId());



        User user=(User)session.getAttribute("user");

        System.out.println(data);
        JSONObject jsonObject=JSONObject.parseObject(data);
        System.out.println(jsonObject.getLong("timestamp")-jsonObject.getLong("nowTime"));

        Activity activity= sqlSession.selectOne("begingRegister",jsonObject.getInteger("activityId"));

        long sub=jsonObject.getLong("nowTime")-jsonObject.getLong("timestamp");

        double latiudec=jsonObject.getDouble("nowLatitude")-jsonObject.getDouble("latitude");
        double longitudec=jsonObject.getDouble("nowLongitude")-jsonObject.getDouble("longitude");
        if(sub>registerTimesub){
            return new CodeEntity(901, "时间不对").toJSONString();
        }
        if(Math.abs(latiudec)>latitudeSub||Math.abs(longitudec)>longitudeSub){
            return new CodeEntity(901, "位置信息不符").toJSONString();
        }

        //判断是否签过到  课程签到事
        Register where=new Register();
        where.setUserId(user.getUserId());
        where.setActivityId(jsonObject.getInteger("activityId"));
       Register register1= sqlSession.selectOne("findRegisterByUserIdAndActivityId",where);
        if(register1!=null){
            if(register1.getRegisterState().equals("旷课")){
                //更新状态 ，需要逻辑判断 updateRegister
                if(activity.getActivityRegisterTime().getTime()>jsonObject.getLong("timestamp")) {

                    register1.setRegisterState("正常");
                }else{
                    register1.setRegisterState("迟到");
                }
                register1.setRegisterTime(new Timestamp(new Date().getTime()));
                register1.setLongitude(jsonObject.getDouble("nowLatitude"));
                register1.setLatiude(jsonObject.getDouble("nowLongitude"));
                sqlSession.update("updateRegister",register1);
                sqlSession.commit();
                return new CodeEntity(200).toJSONString();

            }else {
                //数据库存在签到记录
                if (register1.getRegisterState().length() > 1) {
                    return new CodeEntity(901, "请不要重复签到").toJSONString();
                }
            }

        }

        //逻辑判断  签到状态
        Register register=new Register();
       register.setActivityId(jsonObject.getInteger("activityId"));
       register.setRegisterTime(new Timestamp(jsonObject.getLong("nowTime")));
       register.setUserId(user.getUserId());
       register.setRegisterState("正常");
       sqlSession.insert("addRegister",register);
       sqlSession.commit();
        return new CodeEntity(200).toJSONString();

    }
}
